package com.ddstation.crm.menu.controller;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import com.ddstation.crm.login.bean.SessionLoginData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ddstation.crm.login.util.WnSessionUtil;
import com.ddstation.crm.menu.service.MenuService;
import com.ddstation.security.util.WnBizMsgCrypt;

/**
 * 菜单 针对某一个人菜单的开放配置
 * 问暖配置 + 机构配置 + 个人配置
 * @author zhangdifeng
 * @date 2018年3月28日
 */
@Controller
public class MinusMainController {

	/**
	 * 菜单设置
	 * @param session
	 * @return
	 */
	@RequestMapping(value="wncrm_minus_menu.do/settting/init", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> initMinusMenuSettingEvent(
			@RequestParam String doctorUUID,
			@RequestParam int menuType,
			HttpSession session) {

		// 从session获取登录用户基础数据
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Integer doctorId = Integer.parseInt(WnBizMsgCrypt.decrypt(doctorUUID));
		Map<String, Object> retMap = new HashMap<String, Object>();
		switch (menuType) {
		case 1:
			retMap.put("menus", menuService.initDoctorVerticalMenuSetting(institutionId, doctorId));
			break;
		case 2:
			retMap.put("menus", menuService.intDoctorHorizontalMenuSetting(institutionId, doctorId));
			break;
		default:
			
		}
		retMap.put("errcode", 0);
		return retMap;
	}
	
	/**
	 * 菜单设置
	 * @param session
	 * @return
	 */
	@RequestMapping(value="wncrm_minus_menu.do/settting/save", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> saveMinusMenuSettingEvent(
			@RequestParam String doctorUUID,
			@RequestParam int menuType,
			@RequestParam(name="minusMenus[]", required=false)Integer[]minusMenus,
			HttpSession session) {

		// 从session获取登录用户基础数据
		String institutionId = WnSessionUtil.getInstitutionId(session);
		Integer doctorId = Integer.parseInt(WnBizMsgCrypt.decrypt(doctorUUID));
		Map<String, Object> retMap = new HashMap<String, Object>();
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			retMap.put("errormsg", "无法获取到权限，请尝试重新登录");
			return retMap;
		} else if (loginData.getIsManager() != 1) {
			retMap.put("errormsg", "权限不足，无法执行操作");
			return retMap;
		}
		switch (menuType) {
		case 1:
			menuService.saveDoctorVerticalMenuSetting(institutionId, doctorId, minusMenus);
			break;
		case 2:
			menuService.saveDoctorHorizontalMenuSetting(institutionId, doctorId, minusMenus);
			break;
		default:
			
		}
		retMap.put("errcode", 0);
		return retMap;
	}
	
	@Inject private MenuService menuService = null;
}
